<%@page import="cn.sendto.service.CarInfoService"%>
<%@page import="cn.sendto.model.CarInfoModel"%>
<%@page import="java.util.HashMap"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<%
	request.setCharacterEncoding("UTF-8");

	String SbID = request.getParameter("SbID");
	String Sbname = request.getParameter("Sbname");
	String Sbtype = request.getParameter("Sbtype");
	String Chargeman = request.getParameter("Chargeman");
	String Status = request.getParameter("Status");
	String Department = request.getParameter("Department");

	//验证数据的有效性
	ArrayList<Integer> errlist = new ArrayList<Integer>();
	HashMap<String, String> formlist = new HashMap<String, String>();
	if (SbID == null || SbID.equals("")) {
		errlist.add(1);
	} else {
		formlist.put("carnum", SbID);
	}
	if (Sbname == null || Sbname.equals("")) {
		errlist.add(2);
	} else {
		formlist.put("cartype", Sbname);
	}

	if (Sbtype == null || Sbtype.equals("")) {
		errlist.add(3);
	} else {
		formlist.put("color", Sbtype);
	}
	if (Chargeman == null || Chargeman.equals("")) {
		errlist.add(4);
	} else {
		formlist.put("drivenum", Chargeman);
	}
	if (Status == null || Status.equals("")) {
		errlist.add(4);
	} else {
		formlist.put("Status", Status);
	}
	if (errlist.size() > 0) {
		request.setAttribute("info", errlist);
		request.setAttribute("form", formlist);
		request.getRequestDispatcher("addcar.jsp").forward(request, response);
		return;
	}

	CarInfoModel cim = new CarInfoModel();
	cim.setSbID(Integer.parseInt(SbID));
	cim.setSbname(Sbname);
	cim.setSbtype(Integer.parseInt(Sbtype));
	cim.setChargeman(Chargeman);
	cim.setStatus(Status);
	cim.setDepartment(Department);
	
	CarInfoService cis = new CarInfoService();
	 int rows = cis.add(cim);
	   if(rows > 0){
		    //返回注册页面
		   	 request.setAttribute("addcarinfo", "添加成功，可以继续添加。或者"+"<a href='carlist.jsp'>点击此处返回车辆列表</a>");
		   	 request.getRequestDispatcher("addcar.jsp").forward(request, response);
		   	 return;
		    }else if(rows == -1){
		    	 request.setAttribute("addcarinfo", "添加失败,设备已存在");
		    	 request.setAttribute("form", formlist);
		       	 request.getRequestDispatcher("addcar.jsp").forward(request, response);
		       	 return;
		    }
		    else{
		   	 request.setAttribute("addcarinfo", "添加失败。");
		   	 request.getRequestDispatcher("addcar.jsp").forward(request, response);
		   	 return;
		    }
	%>